package model;

import com.j256.ormlite.jdbc.DataSourceConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
 * @author JS: Juan Sanjuan
 * This Class is an utility 
 * to the system using Spring Security
 */
public class DataBaseHelper
{
    
    private Connection cn;
    private ConnectionSource cns;
    DataSource ds=null;
    
    /**
     * Constructor
     * Create all context to start a Database Connection
     */ 
    public DataBaseHelper()
    {
       try
        {
            InitialContext jndiCntx = new InitialContext();
            Context envCtx = (Context) jndiCntx.lookup("java:comp/env");
            ds = (DataSource) envCtx.lookup("jdbc/sistogeo");
            cn=ds.getConnection();
        } catch (Exception ex)
        {
            Logger.getLogger(DataBaseHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * Constructor with params
     * @param strContext String that represents the context
     * @param strDatasource String that represents the data source
     */ 
    public DataBaseHelper(String strContext, String strDatasource)
    {
        try
        {
            InitialContext jndiCntx = new InitialContext();
            Context envCtx = (Context) jndiCntx.lookup(strContext);
            ds = (DataSource) envCtx.lookup(strDatasource);
            cn=ds.getConnection();
        } catch (Exception ex)
        {
            Logger.getLogger(DataBaseHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
     
    /**
     * @return the current connection
     * or null if the group doesn't exist
     */ 
    public Connection getConection() throws Exception {
        return cn;
    }
     
     /**
     * @return The current ConnectionSOurce
     */ 
     public ConnectionSource getDataSourceConnection() {
        try
        {
            if(cns==null)
            {
                String strMetadata =cn.getMetaData().getURL();
                cns = new DataSourceConnectionSource(ds, strMetadata);
            }
        } catch (Exception ex)
        {
            Logger.getLogger(DataBaseHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
        return cns;
    }
     
     
     /**
     * Close connection and releases all resources to the container
     */ 
     public void CloseConnection()
     {
         try{
             this.cns.close();
             //this.cns.releaseConnection(this.cn);
         }catch(Exception e){
             Logger.getLogger(DataBaseHelper.class.getName()).log(Level.SEVERE, null, e);
         }
         try{
             this.cn.close();
         }catch(Exception e){
             Logger.getLogger(DataBaseHelper.class.getName()).log(Level.SEVERE, null, e);
         }
         try{
             this.ds=null;
         }catch(Exception e){
             Logger.getLogger(DataBaseHelper.class.getName()).log(Level.SEVERE, null, e);
         }
         this.cns=null;
         this.cn=null;
     }
     
     /**
      * Closes the connection but thwows exception on error
      * @throws the exception while trying to close the connections 
      */ 
     public void CloseConnectionThrow() throws SQLException
     {
         this.cn.close();
         this.cns.close();
     }
}
